﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Globalization;
using System.Threading;
using WebMVC.Helpers;
using WebMVC.Models;

using BusinessTier.DataContext;
using System.Data.Entity.Database;

namespace WebSite
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            ///TODO ADD constraints for parameters
            routes.MapRoute(
                "ImageGenerator",
                "ImageFromText/{fontId}/{color}/{size}",
                new { controller = "StaticData", action = "GetImageFromText" },
                new { fontId = @"\d" }
            );

            routes.MapRoute(
                "PriceCalculatorJSON",
                "Price/{quantity}/{mediaCode}/{methodCode}",
                new { controller = "Product", action = "CalculatePriceJSON" }
            );

            routes.MapRoute(
                "PriceCalculator",
                "Product/CalculatePrice",
                new { controller = "Product", action = "CalculatePrice" },
                new string[] { "WebMVC.Controllers" }
            );

            routes.MapRoute(
                "ProductDefault",
                "Product/List/{type}",
                new { controller = "Product", action = "List", type = "BCARD" },
                new string[] {"WebMVC.Controllers"}
            );

            routes.MapRoute(
                "AboutRoute",
                "About",
                new { controller = "Home", action = "About" }
                );
            
            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

            foreach (Route r in routes)
            {
                if (!(r.RouteHandler is SingleLingualMvcRouteHandler))
                {
                    r.RouteHandler = new MultiLingualMvcRouteHandler();
                    r.Url = "{culture}/" + r.Url;
                    if (r.Defaults == null)
                    {
                        r.Defaults = new RouteValueDictionary();
                    }
                    r.Defaults.Add("culture", Culture.ru.ToString());
                    if (r.Constraints == null)
                    {
                        r.Constraints = new RouteValueDictionary();
                    }
                    r.Constraints.Add("culture", new CultureConstraint(Culture.en.ToString(), Culture.ru.ToString(), Culture.hy.ToString()));
                }
            }
        }

        protected void Application_Start()
        {
            DbDatabase.SetInitializer<SimplyData>(new SimplyDataInitializer());
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
        }

      /*  protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session != null)
            {
                CultureInfo ci = (CultureInfo)this.Session["Culture"];
                if (ci == null)
                {
                    //Gets from profile
                    string langName = HttpContext.Current.Profile.GetPropertyValue("Culture").ToString();
                    if (String.IsNullOrEmpty(langName))
                    {                        
                        if (HttpContext.Current.Request.UserLanguages != null && HttpContext.Current.Request.UserLanguages.Length != 0)
                        {
                            //Gets accepted list 
                            langName = HttpContext.Current.Request.UserLanguages[0].Substring(0, 2);
                        }
                        else
                        {
                            //Sets default culture to english neutral
                            langName = "en";
                        }
                        
                        HttpContext.Current.Profile.SetPropertyValue("Culture", langName);
                    }
                    ci = new CultureInfo(langName);
                    this.Session["Culture"] = ci;
                }
                Thread.CurrentThread.CurrentUICulture = ci;
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
            }
        }

        public override string GetVaryByCustomString(HttpContext context, string value)
        {
            if (value.Equals("lang"))
            {
                return Thread.CurrentThread.CurrentUICulture.Name;
            }
            return null;
        }*/
    }
}